package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.ChargeRuleBO;
import com.junzhiit.nengji.cms.service.ChargeRuleService;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.ChargeRuleVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ChargeRuleDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.ChargeRule;

@Service
public class ChargeRuleServiceImpl extends BaseServiceImpl implements ChargeRuleService {

	@Autowired
	private ChargeRuleDAO chargeRuleDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, ChargeRuleVO chargeRuleVO) {
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(chargeRuleVO.getSessionId()), SystemUserSessionVO.class);
		
		ChargeRuleBO chargeRuleBO = new ChargeRuleBO();
		BeanUtils.copyProperties(chargeRuleVO, chargeRuleBO);
		chargeRuleBO.setCsId(systemUserSessionVO.getSystemId());
		
		List<ChargeRuleBO> list = chargeRuleDAO.findAll(chargeRuleBO, dtp.getStart(), dtp.getLength());
		int totalCount = chargeRuleDAO.getTotalCount(chargeRuleBO);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(ChargeRuleVO chargeRuleVO) {
		ResultVO result = new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(chargeRuleVO.getSessionId()), SystemUserSessionVO.class);
		
		ChargeRule chargeRule = new ChargeRule();
		BeanUtils.copyProperties(chargeRuleVO, chargeRule);
		
		chargeRule.setCsId(systemUserSessionVO.getSystemId());
		chargeRuleDAO.insert(chargeRule);
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		ChargeRuleBO chargeRule = chargeRuleDAO.findById(id);
		result.setData(chargeRule);
		return result;
	}

	@Override
	public ResultVO update(ChargeRuleVO chargeRuleVO) {
		ResultVO result = new ResultVO();
		ChargeRule chargeRule = chargeRuleDAO.query(chargeRuleVO.getId());
		BeanUtils.copyProperties(chargeRuleVO, chargeRule);
		chargeRuleDAO.update(chargeRule,true);
		return result;
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		chargeRuleDAO.delete(id);
		return result;
	}
}
